const API_BASE_URL = "http://localhost:8080";

export interface BlogArticle {
  id: number;
  title: string;
  content: string;
  categoryId: number | null;
  tags?: string;
  status: number;
  createTime: string;
  updateTime: string;
}

export interface Category {
  id: number;
  name: string;
  create_time: string;
}

// 定义响应数据类型
export interface BlogArticleResponse {
  code: number;
  rows: BlogArticle[];
  msg: string;
}

export interface ResponseData<T> {
  code: number;
  rows: T[];
  data: T;
  msg: string;
}

// 修改 getBlogArticles 函数以支持查询参数
export async function getBlogArticles(
  pageSize: number,
  pageNum: number
): Promise<BlogArticle[]> {
  const url = new URL(`${API_BASE_URL}/system/blogArticle/list`);
  url.searchParams.append("pageNum", pageNum.toString());
  url.searchParams.append("pageSize", pageSize.toString());

  const res = await fetch(url.toString());
  const responseData: ResponseData<BlogArticle> = await res.json();
  return responseData.rows;
}

export async function getBlogArticleById(id: string): Promise<BlogArticle> {
  const res = await fetch(`${API_BASE_URL}/system/blogArticle/${id}`);
  const responseData: ResponseData<BlogArticle> = await res.json();
  console.log("🚀 ~ getBlogArticleById ~ responseData:", responseData);
  return responseData.data;
}

export async function getCategories(
  pageSize: number,
  pageNum: number
): Promise<Category[]> {
  const url = new URL(`${API_BASE_URL}/system/category/list`);
  url.searchParams.append("pageNum", pageNum.toString());
  url.searchParams.append("pageSize", pageSize.toString());

  const res = await fetch(url.toString());
  const responseData: ResponseData<Category> = await res.json();
  return responseData.rows;
}
